<?php


namespace models\Business;


use models\Exception\Business\AnnounceException;
use models\Exception\Business\AnnouncestatsException;
use models\Exception\DAO\ModelDriverException;
use models\Exception\DAO\ModelException;
use models\Exception\DAO\ModelReflectionException;
use models\Exception\DAO\ModelSqlException;
use models\Exception\Service\ServiceException;
use models\Exception\Transformer\TransformerException;
use models\Service\AnnounceService;
use models\Service\AnnouncestatsService;
use Yaf\Registry;

class Announce
{
    /**
     * 删除消息
     * @param $parameters
     * @throws AnnounceException
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ServiceException
     */
    static public function delete($parameters) {
        try {
            if (! isset($parameters['id']) || empty($parameters['id']))
                throw new AnnounceException(AnnounceException::ANNOUNCE_ID_IS_EMPTY, AnnounceException::ANNOUNCE_ID_IS_EMPTY_NO);

            AnnounceService::getInstance()->delete($parameters['id']);
        } catch (AnnounceException $e) {
            throw $e;
        } catch (ModelDriverException $e) {
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        }
    }

    /**
     * 阅读消息
     * @param array $parameters
     * @throws AnnouncestatsException
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws TransformerException
     */
    static public function readAnnounce($parameters = []) {

        try {
            // $app_id = 0, $announce_id = 0, $member_id = 0
            AnnouncestatsService::getInstance()->create($parameters['app_id'], $parameters['id'], Registry::get('jwt_info')->id);
        } catch (ModelDriverException $e) {
            if ($e->getCode() == ModelDriverException::DRIVER_EXCEPTION_MSG_NO)
                throw new AnnouncestatsException(AnnouncestatsException::ANNOUNCE_READ_FAILURE, AnnouncestatsException::ANNOUNCE_READ_FAILURE_NO);
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }
    }

    /**
     * 编辑
     * @param array $parameters
     * @return array|mixed|null
     * @throws AnnounceException
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ServiceException
     * @throws TransformerException
     */
    static public function edit($parameters = []) {
        try {
            if (! isset($parameters['id']) || empty($parameters['id']))
                throw new AnnounceException(AnnounceException::ANNOUNCE_ID_IS_EMPTY, AnnounceException::ANNOUNCE_ID_IS_EMPTY_NO);

            $update_arr = [];
            if (isset($parameters['title']))
                $update_arr['title'] = $parameters['title'];
            if (isset($parameters['stats']))
                $update_arr['stats'] = $parameters['stats'];
            if (isset($parameters['content']))
                $update_arr['content'] = $parameters['content'];
            if (isset($parameters['app_id']))
                $update_arr['app_id'] = $parameters['app_id'];

            if (!empty($update_arr)) {
                $update_arr['updated_at'] = date('Y-m-d H:i:s', time());
                $update_arr['updated_by'] = Registry::get('jwt_info')->id;

                $announce = AnnounceService::getInstance()->update($update_arr, $parameters['id']);

            } else
                $announce = AnnounceService::getInstance()->getOne($parameters['id']);

            return $announce;
        } catch (AnnounceException $e) {
            throw $e;
        } catch (ModelDriverException $e) {
            if ($e->getCode() == ModelDriverException::DRIVER_EXCEPTION_MSG_NO)
                throw new AnnounceException(AnnounceException::ANNOUNCE_EDIT_FAILURE, AnnounceException::ANNOUNCE_EDIT_FAILURE_NO);
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }

    }

    /**
     * @param array $parameters
     * @return array|mixed
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ServiceException
     * @throws TransformerException
     */
    static public function getAnnounceList($parameters = []) {
        try {
            $rules = $condition = [];
            if (isset($parameters['app_id']))
                array_push($rules, ['field' => 'app_id', 'op' => 'eq', 'data' => $parameters['app_id']]);

            if (isset($parameters['stats']))
                array_push($rules, ['field' => 'stats', 'op' => 'eq', 'data' => $parameters['stats']]);

            if (isset($parameters['title']))
                array_push($rules, ['field' => 'stats', 'op' => 'cn', 'data' => $parameters['title'] . '%']);

            if (!empty($rules))
                $condition = ['groupOp' => 'AND', 'rules' => $rules];

            $announce = AnnounceService::getInstance()->getList(
                isset($parameters['page']) ? $parameters['page'] : 1,
                isset($parameters['page_size']) ? $parameters['page_size'] : 10,
                isset($parameters['sort']) ? $parameters['sort'] : 'desc',
                isset($parameters['order']) ? $parameters['order'] : 'id',
                $condition
            );

            if (isset($parameters['from_api']) && $parameters['from_api'] === true && !empty($announce)) {
                $stats = AnnouncestatsService::getInstance()->getList(1, count($announce['data']), 'desc', 'id',
                    ['groupOp' => 'AND', 'rules' => [
                        ['field' => 'app_id', 'op' => 'eq', 'data' => $parameters['app_id']],
                        ['field' => 'member_id', 'op' => 'eq', 'data' => Registry::get('jwt_info')->id],
                        ['field' => 'announce_id', 'op' => 'in', 'data' => implode(',', array_column($announce['data'], 'id'))]
                    ]]);
                $stats_id_arr = array_column($stats['data'], 'id', 'announce_id');

                foreach ($announce['data'] as $idx => $announce_info) {
                    $announce['data'][$idx]['is_read'] = (int)isset($stats_id_arr[$announce_info['id']]);
                }
            } else
                $announce = User::getUsersByModule($announce);

            return $announce;
        } catch (ModelDriverException $e) {
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }

    }

    /**
     * @param array $parameters
     * @return array
     * @throws AnnounceException
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws TransformerException
     */
    static public function create($parameters = []) {
        try {
            $date_now = date('Y-m-d H:i:s', time());
            // $app_id = 0, $title = "", $content = "", $stats = 0, $created_at = "", $created_by = 0, $updated_at = "", $updated_by = 0,

            return AnnounceService::getInstance()->create(
                $parameters['app_id'], $parameters['title'], $parameters['content'], $parameters['stats'],
                $date_now, Registry::get('jwt_info')->id, $date_now, Registry::get('jwt_info')->id
            );
        } catch (ModelDriverException $e) {
            if ($e->getCode() == ModelDriverException::DRIVER_EXCEPTION_MSG_NO)
                throw new AnnounceException(AnnounceException::ANNOUNCE_CREATE_FAILURE, AnnounceException::ANNOUNCE_CREATE_FAILURE_NO);
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }
    }

}
